package com.bawei.a2010douyin.ui.push;

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bawei.a2010douyin.R;
import com.bawei.a2010douyin.base.mvp.BaseMvpActivity;
import com.bawei.a2010douyin.base.utils.ARouterPath;
import com.bawei.a2010douyin.entity.PushEntity;
import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.tencent.live2.V2TXLiveDef;
import com.tencent.live2.V2TXLivePusher;
import com.tencent.live2.impl.V2TXLivePusherImpl;
import com.tencent.rtmp.ui.TXCloudVideoView;

import java.util.HashMap;

/***

 点击开始直播：1。 网络请求获得推流地址 2。推流地址获得成功后开始推流


 */
@Route(path = ARouterPath.PUSH_AROUTER_PATH)
public class PushActivity extends BaseMvpActivity<PushContract.IPushView,PushPresenter> implements PushContract.IPushView {
    private TXCloudVideoView pusherTxCloudView;
    private EditText etCaption;
    private V2TXLivePusher mLivePusher;




    @Override
    public int bindLayout() {
        return R.layout.activity_push;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        pusherTxCloudView = (TXCloudVideoView) findViewById(R.id.pusher_tx_cloud_view);
        etCaption = (EditText) findViewById(R.id.et_caption);
        // 指定对应的直播协议为 RTMP，该协议不支持连麦
         mLivePusher = new V2TXLivePusherImpl(this, V2TXLiveDef.V2TXLiveMode.TXLiveMode_RTMP);
        //开启摄像头预览
        mLivePusher.setRenderView(pusherTxCloudView);
        mLivePusher.startCamera(true);
        mLivePusher.startMicrophone();

    }

    @Override
    public PushPresenter createPresenter() {
        return new PushPresenter();
    }

    @Override
    public void initData() {

    }
    //请求数据成功获得推流地址
    @Override
    public void showStartPush(PushEntity data) {
        String url_push = data.getUrl_push();
        if(!TextUtils.isEmpty(url_push)){
            int ret = mLivePusher.startPush(url_push);
            if (ret == -1) {
                Log.i("TAG", "startRTMPPush: license 校验失败");
            }else{
                Toast.makeText(this, "推流成功", Toast.LENGTH_SHORT).show();
            }

        }


    }
    //请求下播接口数据
    @Override
    public void showStopPush(String data) {
        Toast.makeText(this, "下播成功", Toast.LENGTH_SHORT).show();
    }

    //点击事件开始直播
    public void startPush(View view) {
        HashMap<String, String> map = new HashMap<>();
        map.put("address", SPUtils.getInstance().getString("location"));
        map.put("caption",etCaption.getText().toString());
        String s = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), s);
        mPresenter.startPush(requestBody);
    }
    //点击事件退出直播
    public void stopPush(View view) {
        mPresenter.stopPush();
    }
}